<?php
/**
 * Created by PhpStorm.
 * User: xiangfeng
 * Date: 2020/8/1
 * Time: 10:48 AM
 */

namespace app\index\controller;




use think\admin\Controller;
use think\admin\extend\DataExtend;
use think\App;

class Base extends Controller
{
    protected $params;
    protected $redis;
    protected $expire;

    protected function initialize()
    {
        parent::initialize();
        $this->params = $this->request->param();
        //$this->redis = SimpleModelConnectRedis::getInstance();
        $this->expire = 60 * 60 * 1;
    }

    function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 查询文章分类
     * @param int $id
     * @return array|null|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function findArticleTypeInfo(int $id)
    {
        $data = $this->app->db->name('jh_article_type')->field('id,name,bg_image')->where(['id' => $id])->find();
        return $data;
    }

    /**
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function getArticleTypeList()
    {
        $data = $this->app->db->name('jh_article_type')->field('id,name,bg_image')->where(['status' => 1])->select();
        return $data;
    }

    /**
     * 上一篇文章
     * @param int $id
     * @param int $article_type_id
     * @return array|null|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function findPrevArticleInfo(int $id, int $article_type_id)
    {
        $where = [
            ['id', '<', $id],
            ['article_type_id', '=', $article_type_id],
            ['status', '=', 1],
            ['delete_time', '=', 0],
        ];
        $field = 'id,title';
        $data = $this->app->db->name('jh_article')->field($field)->where($where)->order('id desc')->find();
        return $data;
    }

    /**
     * 下一篇文章
     * @param int $id
     * @param int $article_type_id
     * @return array|null|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function findNextArticleInfo(int $id, int $article_type_id)
    {
        $where = [
            ['id', '>', $id],
            ['article_type_id', '=', $article_type_id],
            ['status', '=', 1],
            ['delete_time', '=', 0],
        ];
        $field = 'id,title';
        $data = $this->app->db->name('jh_article')->field($field)->where($where)->order('id desc')->find();
        return $data;
    }

    /**
     * 相关信息推荐
     * @param int $id
     * @param int $article_type_id
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function recommendArticleList(int $id, int $article_type_id)
    {
        $where = [
            'article_type_id' => $article_type_id,
            'status' => 1,
            'delete_time' => 0
        ];
        $field = 'id,title';
        $data = $this->app->db->name('jh_article')->field($field)->where($where)->orderRand()->limit(6)->select();
        return $data;
    }

    /**
     * 轮播图列表
     * @param int $slide_id
     * @param int $limit
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function findAllSlideItem(int $slide_id, int $limit = 1)
    {
        $cache_key = SLIDE_INFO . '_' . $slide_id;
        $data = $this->app->cache->get($cache_key);
        if (empty($data)) {
            $data = $this->app->db->name('jh_slide_item')->field('title,image,url,slide_describe,eng_title')->where(['slide_id' => $slide_id, 'status' => 1])->order('des desc')->limit($limit)->cache($this->expire - rand(0, 100))->select();
            $data = !empty($data) ? $data->toArray() : [];
            $this->app->cache->set($cache_key, serialize($data), $this->expire - rand(0, 100));
        } else {
            $data = unserialize($data);
        }
        return $data;
    }

    /**
     * 合作伙伴
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function getPartnersList()
    {
        $slide_id = 2;
        $cache_key = SLIDE_INFO . '_' . $slide_id;
        $data = $this->app->cache->get($cache_key);
        if (empty($data)) {
            $data = $this->app->db->name('jh_slide_item')->field('title,image,url')->where(['slide_id' => 2, 'status' => 1])->order('des desc')->limit(24)->select();
            $partner = [];
            if (!empty($data)) {
                $data = $data->toArray();
                $offset = 6;
                for ($i = 0; $i < ceil(count($data) / $offset); $i++) {
                    $partner[] = array_slice($data, $i * $offset, $offset);
                }
            }
            $data = !empty($partner) ? $partner : [];
            $this->app->cache->set($cache_key, serialize($data), $this->expire - rand(10, 200));
        } else {
            $data = unserialize($data);
        }
        return $data;
    }

    /**
     * @param int $id
     * @return array|null|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function findNewsMenuById(int $id)
    {
        $where = [
            'id' => $id
        ];
        $field = 'id,name,cover_image,eng_name';
        $data = $this->app->db->name('jh_article_type')->field($field)->cache(7200)->where($where)->find();
        return $data;
    }

    /**
     * 查询子分类ID
     * @param int $article_type_id
     * @return string
     */
    protected function getNewTypeSubIds(int $article_type_id)
    {
        $type_data = $this->app->db->name('jh_article_type')->where(['parent_id' => $article_type_id])->column('id,parent_id,name,bg_image,sorts,status', 'id');
        $ids = join(',', DataExtend::getArrSubIds($type_data, $article_type_id, 'id', 'parent_id'));
        return $ids;
    }

    /**
     * @param int $article_type_id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function getNewTypePid(int $article_type_id)
    {
        $list = $this->app->db->name('jh_article_type')->field('id,parent_id')->where(['id' => $article_type_id])->find();
        if ($list['parent_id'] != 0) {
            return $this->getNewTypePid($list['parent_id']);
        } else {
            return $list['id'];
        }

    }

    /**
     * 查询分类和子分类最新文章
     * @param int $article_type_id
     * @param int $limit
     * @return array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function findNewArticleByTypeId(int $article_type_id, int $limit = 3)
    {
        $cache_key = ARTICLE_NEW_INFO . '_' . $article_type_id . '_' . $limit;
        $data = $this->app->cache->get($cache_key);
        if (empty($data)) {
            $ids = $this->getNewTypeSubIds($article_type_id);
            $ids_arr = explode(",", $ids);
            $where = [
                ['status', '=', 1],
                ['article_type_id', count($ids_arr) > 1 ? 'in' : '=', $ids],
                ['delete_time', '=', 0],
            ];
            $field = 'id,title,describes,image,create_time,content';
            $data = $this->app->db->name('jh_article')->field($field)->order('sorts desc,create_time desc')->where($where)->limit($limit)->cache(60 *60 * 2)->select();
            if (empty($data)) {
                $this->app->cache->set($cache_key, serialize($data), 1);
                return [];
            }
            $data = $data->toArray();
            foreach ($data as &$val) {
                $val['create_date'] = date('Y-m', $val['create_time']);
                $val['create_day'] = date('d', $val['create_time']);
                unset($val['create_time']);
            }
            $this->app->cache->set($cache_key, serialize($data), $this->expire - rand(10, 200));
        } else {
            $data = unserialize($data);
        }
        return $data;
    }

    /**
     * 查询下级分类列表
     * @param int $article_type_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function getNextArticleTypeList(int $article_type_id)
    {
        $data = $this->app->db->name('jh_article_type')->field('id,name,parent_id')->where(['parent_id' => $article_type_id, 'status' => 1])->order('sorts desc')->select();
        return !empty($data) ? $data->toArray() : [];
    }
}